﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using Gman.WebApp.OperacionServiceRef;
using Gman.Entity.Entities;
using Gman.WebApp.ServiceControllers;
using AutoMapper;

namespace Gman.WebApp.ServiceControllers
{
    public class OperacionServiceController : ServiceControllerBase
    {
        #region Sede

        public int RegistrarSede(SedeBE sede)
        {
            try
            {
                Request_Sede rqt = new Request_Sede();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "AddSede" };
                rqt.SedeDTO = Mapper.Map<SedeBE, SedeBE>(sede);

                Response_Sede response = OperacionServiceClient.RegistrarSede(rqt);

                if (rqt.SedeDTO.IDSede > 0)
                { sede.IDSede = rqt.SedeDTO.IDSede; }

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsSede;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public IList<SedeBE> ListarSede(SedeBE sede)
        {
            try
            {
                Request_Sede rqt = new Request_Sede();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "ListSede" };
                rqt.SedeDTO = Mapper.Map<SedeBE, SedeBE>(sede);

                Response_Sede response = OperacionServiceClient.ListarSedes(rqt);

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstSede;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public int ActualizarSede(SedeBE sede)
        {
            try
            {
                Request_Sede rqt = new Request_Sede();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "UpdSede" };
                rqt.SedeDTO = Mapper.Map<SedeBE, SedeBE>(sede);

                Response_Sede response = OperacionServiceClient.ActualizarSede(rqt);

                if (rqt.SedeDTO.IDSede > 0)
                { sede.IDSede = rqt.SedeDTO.IDSede; }

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdSede;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public SedeBE ObtenerSede(SedeBE sede)
        {
            try
            {
                Request_Sede rqt = new Request_Sede();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "GetSede" };
                rqt.SedeDTO = Mapper.Map<SedeBE, SedeBE>(sede);

                Response_Sede response = OperacionServiceClient.ObtenerSede(rqt);

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetSede;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public int EliminarSede(SedeBE sede)
        {
            try
            {
                Request_Sede rqt = new Request_Sede();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "DelSede" };
                rqt.SedeDTO = Mapper.Map<SedeBE, SedeBE>(sede);

                Response_Sede response = OperacionServiceClient.EliminarSede(rqt);

                if (rqt.SedeDTO.IDSede > 0)
                { sede.IDSede = rqt.SedeDTO.IDSede; }

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsSede;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        #endregion


        #region Bien

        public int RegistrarBien(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "AddBien" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.RegistrarBien(request);

                if (request.BienDTO.IDBien > 0)
                {
                    bien.IDBien = request.BienDTO.IDBien;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsBien;
            }
            catch (Exception ex)
            {
                String message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public IList<BienBE> ListarBien(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListBien" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.ListarBien(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstBien;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public BienBE ObtenerBien(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetBien" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.ObtenerBien(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetBien;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int ActualizarBien(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "UpdBien" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.ActualizarBien(request);

                if (request.BienDTO.IDBien > 0)
                {
                    bien.IDBien = request.BienDTO.IDBien;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdBien;
            }
            catch (Exception ex)
            {
                String message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion

        #region Personal Sede - Jamie

        public int RegistrarPersonal(PersonalBE personal)
        {
            try
            {
                Request_Personal rqt = new Request_Personal();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "AddPersonal" };
                rqt.PersonalDTO = Mapper.Map<PersonalBE, PersonalBE>(personal);

                Response_Personal response = OperacionServiceClient.RegistrarPersonal(rqt);

                if (rqt.PersonalDTO.IDPersonal > 0)
                { personal.IDPersonal = rqt.PersonalDTO.IDPersonal; }

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsPersonal;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public IList<PersonalBE> ListarPersonal(PersonalBE personal)
        {
            try
            {
                Request_Personal rqt = new Request_Personal();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "ListPersonal" };
                rqt.PersonalDTO = Mapper.Map<PersonalBE, PersonalBE>(personal);

                Response_Personal response = OperacionServiceClient.ListarPersonal(rqt);

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstPersonal;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public int ActualizarPersonal(PersonalBE personal)
        {
            try
            {
                Request_Personal rqt = new Request_Personal();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "UpdPersonal" };
                rqt.PersonalDTO = Mapper.Map<PersonalBE, PersonalBE>(personal);

                Response_Personal response = OperacionServiceClient.ActualizarPersonal(rqt);

                if (rqt.PersonalDTO.IDPersonal > 0)
                { personal.IDPersonal = rqt.PersonalDTO.IDPersonal; }

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdPersonal;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public PersonalBE ObtenerPersonal(PersonalBE oItem)
        {
            try
            {
                Request_Personal request = new Request_Personal();
                request.PersonalDTO = oItem;
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "GetPersonal" };

                Response_Personal response = OperacionServiceClient.ObtenerPersonal(request);

                return response.GetPersonal;
            }
            catch (Exception ex)
            {
                throw new Exception("PersonalBL.Obtener()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }
        }


        public int EliminarPersonal(PersonalBE personal)
        {
            try
            {
                Request_Personal rqt = new Request_Personal();
                rqt.RequestId = NewRequestId;
                rqt.AccessToken = AccessToken;
                rqt.ClientTag = ClientTag;
                rqt.LoadOptions = new string[] { "DelPersonal" };
                rqt.PersonalDTO = Mapper.Map<PersonalBE, PersonalBE>(personal);

                Response_Personal response = OperacionServiceClient.EliminarPersonal(rqt);

                if (rqt.PersonalDTO.IDPersonal > 0)
                { personal.IDPersonal = rqt.PersonalDTO.IDPersonal; }

                if (rqt.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.DelPersonal;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        #endregion

        #region Proveedor - CMendez

        public Response_Proveedor ProveedorById(ProveedorBE oItem, UsuariosBE user)
        {
            try
            {
                Request_Proveedor request = new Request_Proveedor();
                PersonalBE pers = new PersonalBE();
                request.ProveedorDTO = oItem;
                request.RequestId = NewRequestId;


                request.PersonalBE =
                    new PersonalBE() { IDOrigen = oItem.IDProveedor, OrigenPers = 2, IDEmp = user.IDEmp };
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "Listar" };

                //request.ProveedorDTO = Mapper.Map<ProveedorBE, ProveedorBE>(oItem);
                Response_Proveedor response = OperacionServiceClient.GetProveedorById(request);

                return response;
            }
            catch (Exception ex)
            {
                throw new Exception("ProveedorBL.Obtener()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }

        }



        public int Registrarproveedor(ProveedorBE proveedor, List<PersonalBE> list)
        {
            try
            {
                Request_Proveedor request = new Request_Proveedor();
                request.ProveedorDTO = proveedor;

                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;


                request.listPersonalBE = list.ToArray();
                request.LoadOptions = new string[] { "Add" };
                Response_Proveedor response = OperacionServiceClient.RegistrarProveedor(request);

                if (request.ProveedorDTO.IDProveedor > 0)
                { proveedor.IDProveedor = request.ProveedorDTO.IDProveedor; }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsProveedor;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public IList<ProveedorBE> ListProveedor(ProveedorBE oItem)
        {
            try
            {
                Request_Proveedor request = new Request_Proveedor();
                request.RequestId = NewRequestId;
                //request.ProveedorDTO.RucProv = oItem.RucProv;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "Listar" };
                //request.ProveedorDTO = Mapper.Map<ProveedorBE, ProveedorBE>(oItem);
                Response_Proveedor response = OperacionServiceClient.ListProveedor(request);

                return response.LstProveedor;
            }
            catch (Exception ex)
            {
                throw new Exception("ProveedorBL.ListProveedor()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }

        }

        public IList<ProveedorBE> BuscarProveedor(ProveedorBE oItem)
        {
            try
            {
                Request_Proveedor request = new Request_Proveedor();
                request.RequestId = NewRequestId;
                request.ProveedorDTO = oItem;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "Buscar" };
                //request.ProveedorDTO = Mapper.Map<ProveedorBE, ProveedorBE>(oItem);
                Response_Proveedor response = OperacionServiceClient.BuscarProveedor(request);

                return response.LstProveedor;
            }
            catch (Exception ex)
            {
                throw new Exception("ProveedorBL.BuscarProveedor()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }

        }




        public Response_Proveedor ProveedorByRuc(ProveedorBE oItem, UsuariosBE user)
        {
            try
            {
                Request_Proveedor request = new Request_Proveedor();
                request.ProveedorDTO = oItem;
                request.RequestId = NewRequestId;

                request.PersonalBE =
                    new PersonalBE() { OrigenPers = 2, IDEmp = user.IDEmp };

                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "Listar" };
                //request.ProveedorDTO = Mapper.Map<ProveedorBE, ProveedorBE>(oItem);
                Response_Proveedor response = OperacionServiceClient.GetProveedorByRuc(request);

                return response;
            }
            catch (Exception ex)
            {
                throw new Exception("ProveedorBL.ObtenerXRuc()" + " - " + ex.Message + " - " + ex.InnerException, ex);
            }

        }


        public int Actualizarproveedor(ProveedorBE proveedor, List<PersonalBE> list, List<Int32?> toDelete)
        {
            try
            {
                Request_Proveedor request = new Request_Proveedor();
                request.ProveedorDTO = proveedor;

                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;


                request.listPersonalBE = list.ToArray();
                request.toDelete = toDelete.ToArray();

                request.LoadOptions = new string[] { "update" };
                Response_Proveedor response = OperacionServiceClient.ActualizarProveedor(request);

                if (request.ProveedorDTO.IDProveedor > 0)
                { proveedor.IDProveedor = request.ProveedorDTO.IDProveedor; }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdProveedor;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }



        #endregion

        #region ImagenBien

        public int RegistrarImagenBien(ImagenBienBE Imagenbien)
        {
            try
            {
                Request_ImagenBien request = new Request_ImagenBien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "AddImagenBien" };
                request.ImagenBienDTO = Mapper.Map<ImagenBienBE, ImagenBienBE>(Imagenbien);

                Response_ImagenBien response = OperacionServiceClient.RegistrarImagenBien(request);

                if (request.ImagenBienDTO.IDImagenBien > 0)
                {
                    Imagenbien.IDImagenBien = request.ImagenBienDTO.IDImagenBien;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsImagenBien;
            }
            catch (Exception ex)
            {
                String message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public IList<ImagenBienBE> ListarImagenBien(ImagenBienBE imagenBien)
        {
            try
            {
                Request_ImagenBien request = new Request_ImagenBien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListImagenBien" };
                request.ImagenBienDTO = Mapper.Map<ImagenBienBE, ImagenBienBE>(imagenBien);

                Response_ImagenBien response = OperacionServiceClient.ListarImagenBien(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstImagenBien;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public ImagenBienBE ObtenerImagenBien(ImagenBienBE imagenBien)
        {
            try
            {
                Request_ImagenBien request = new Request_ImagenBien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetImagenBien" };
                request.ImagenBienDTO = Mapper.Map<ImagenBienBE, ImagenBienBE>(imagenBien);

                Response_ImagenBien response = OperacionServiceClient.ObtenerImagenBien(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetImagenBien;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion

    
         #region Material

        public IList<MaterialBE> ListarStockMaterial(MaterialBE material)
            {
                try
                {
                    Request_Material rqt = new Request_Material();
                    rqt.RequestId = NewRequestId;
                    rqt.AccessToken = AccessToken;
                    rqt.ClientTag = ClientTag;
                    rqt.LoadOptions = new string[] { "ListStockMaterial" };
                    rqt.MaterialDTO = Mapper.Map<MaterialBE, MaterialBE>(material);

                    Response_Material response = OperacionServiceClient.ListarStockMaterial(rqt);

                    if (rqt.RequestId != response.CorrelationId)
                        throw new ApplicationException("Error");

                    return response.LstStockMaterial;
                }
                catch (Exception ex)
                {
                    string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                    throw new FaultException(message);
                }
            }

            public MaterialBE ObtenerMaterial(MaterialBE material)
            {
                try
                {
                    Request_Material rqt = new Request_Material();
                    rqt.RequestId = NewRequestId;
                    rqt.AccessToken = AccessToken;
                    rqt.ClientTag = ClientTag;
                    rqt.LoadOptions = new string[] { "GetMaterial" };
                    rqt.MaterialDTO = Mapper.Map<MaterialBE, MaterialBE>(material);

                    Response_Material response = OperacionServiceClient.ObtenerMaterial(rqt);

                    if (rqt.RequestId != response.CorrelationId)
                        throw new ApplicationException("Error");

                    return response.GetMaterial;
                }
                catch (Exception ex)
                {
                    string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                    throw new FaultException(message);
                }
            }

            public int RegistrarMaterial(MaterialBE material)
            {
                try
                {
                    Request_Material rqt = new Request_Material();
                    rqt.RequestId = NewRequestId;
                    rqt.AccessToken = AccessToken;
                    rqt.ClientTag = ClientTag;
                    rqt.LoadOptions = new string[] { "AddMaterial" };
                    rqt.MaterialDTO = Mapper.Map<MaterialBE, MaterialBE>(material);

                    Response_Material response = OperacionServiceClient.RegistrarMaterial(rqt);

                    if (rqt.MaterialDTO.IDSede > 0)
                    { material.IDMaterial = rqt.MaterialDTO.IDMaterial; }

                    if (rqt.RequestId != response.CorrelationId)
                        throw new ApplicationException("Error");

                    return response.InsMaterial;
                }
                catch (Exception ex)
                {
                    string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                    throw new FaultException(message);
                }
            }

            public int ActualizarMaterial(MaterialBE material)
            {
                try
                {
                    Request_Material rqt = new Request_Material();
                    rqt.RequestId = NewRequestId;
                    rqt.AccessToken = AccessToken;
                    rqt.ClientTag = ClientTag;
                    rqt.LoadOptions = new string[] { "UpdMaterial" };
                    rqt.MaterialDTO = Mapper.Map<MaterialBE, MaterialBE>(material);

                    Response_Material response = OperacionServiceClient.ActualizarMaterial(rqt);

                    if (rqt.MaterialDTO.IDSede > 0)
                    { material.IDMaterial = rqt.MaterialDTO.IDMaterial; }

                    if (rqt.RequestId != response.CorrelationId)
                        throw new ApplicationException("Error");

                    return response.UpdMaterial;
                }
                catch (Exception ex)
                {
                    string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                    throw new FaultException(message);
                }
            }


         #endregion
    
    

        #region BajaActivo

            public int RegistrarBajaActivo(BajaActivoBE bajaActivo, List<BajaActivoDetalleBE> lista)
            {
                try
                {
                    Request_BajaActivo request = new Request_BajaActivo();
                    request.RequestId = NewRequestId;
                    request.AccessToken = AccessToken;
                    request.ClientTag = ClientTag;
                    request.LoadOptions = new string[] { "AddBajaActivo" };
                    request.BajaActivoDTO = Mapper.Map<BajaActivoBE, BajaActivoBE>(bajaActivo);

                    request.listBajaActivoDetalleBE = lista.ToArray();

                    Response_BajaActivo response = OperacionServiceClient.RegistrarBajaActivo(request);

                    if (request.BajaActivoDTO.IDBajaActivo > 0)
                    {
                        bajaActivo.IDBajaActivo = request.BajaActivoDTO.IDBajaActivo;
                    }

                    if (request.RequestId != response.CorrelationId)
                        throw new ApplicationException("Error");

                    return response.InsBajaActivo;
                }
                catch (Exception ex)
                {
                    string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                    throw new FaultException(message);
                }
            }


        public IList<BajaActivoBE> ListarBajaActivo(BajaActivoBE bajaActivo)
        {
            try
            {
                Request_BajaActivo request = new Request_BajaActivo();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListBajaActivo" };
                request.BajaActivoDTO = Mapper.Map<BajaActivoBE, BajaActivoBE>(bajaActivo);

                Response_BajaActivo response = OperacionServiceClient.ListarBajaActivo(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstBajaActivo;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public BajaActivoBE ObtenerBajaActivo(BajaActivoBE bajaActivo)
        {
            try
            {
                Request_BajaActivo request = new Request_BajaActivo();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetBajaActivo" };
                request.BajaActivoDTO = Mapper.Map<BajaActivoBE, BajaActivoBE>(bajaActivo);

                Response_BajaActivo response = OperacionServiceClient.ObtenerBajaActivo(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetBajaActivo;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int ActualizarTransferenciaActivo(BajaActivoBE bajaActivo, List<BajaActivoDetalleBE> list, List<BajaActivoDetalleBE> toDelete)
        {
            try
            {
                Request_BajaActivo request = new Request_BajaActivo();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "UpdBajaActivo" };
                request.BajaActivoDTO = Mapper.Map<BajaActivoBE, BajaActivoBE>(bajaActivo);

                request.listBajaActivoDetalleBE = list.ToArray();
                request.toDelete = toDelete.ToArray();


                Response_BajaActivo response = OperacionServiceClient.ActualizarBajaActivo(request);

                if (request.BajaActivoDTO.IDBajaActivo > 0)
                {
                    bajaActivo.IDBajaActivo = request.BajaActivoDTO.IDBajaActivo;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdBajaActivo;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion

        #region BajaActivoDetalle

        public int RegistrarBajaActivoDetalle(BajaActivoDetalleBE bajaActivoDetalle)
        {
            try
            {
                Request_BajaActivoDetalle request = new Request_BajaActivoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "AddBajaActivoDetalle" };
                request.BajaActivoDetalleDTO = Mapper.Map<BajaActivoDetalleBE, BajaActivoDetalleBE>(bajaActivoDetalle);

                Response_BajaActivoDetalle response = OperacionServiceClient.RegistrarBajaActivoDetalle(request);

                if (request.BajaActivoDetalleDTO.IDBajaActivo > 0)
                {
                    bajaActivoDetalle.IDBajaActivo = request.BajaActivoDetalleDTO.IDBajaActivo;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsBajaActivoDetalle;
            }
            catch (Exception ex)
            {
                String message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public IList<BajaActivoDetalleBE> ListarBajaActivoDetalle(BajaActivoDetalleBE bajaActivoDetalle)
        {
            try
            {
                Request_BajaActivoDetalle request = new Request_BajaActivoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListBajaActivoDetalle" };
                request.BajaActivoDetalleDTO = Mapper.Map<BajaActivoDetalleBE, BajaActivoDetalleBE>(bajaActivoDetalle);

                Response_BajaActivoDetalle response = OperacionServiceClient.ListarBajaActivoDetalle(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstBajaActivoDetalle;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public BajaActivoDetalleBE ObtenerBajaActivoDetalle(BajaActivoDetalleBE bajaActivoDetalle)
        {
            try
            {
                Request_BajaActivoDetalle request = new Request_BajaActivoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetBajaActivoDetalle" };
                request.BajaActivoDetalleDTO = Mapper.Map<BajaActivoDetalleBE, BajaActivoDetalleBE>(bajaActivoDetalle);

                Response_BajaActivoDetalle response = OperacionServiceClient.ObtenerBajaActivoDetalle(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetBajaActivoDetalle;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int ActualizarBajaActivoDetalle(BajaActivoDetalleBE bajaActivoDetalle)
        {
            try
            {
                Request_BajaActivoDetalle request = new Request_BajaActivoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "UpdBajaActivoDetalle" };
                request.BajaActivoDetalleDTO = Mapper.Map<BajaActivoDetalleBE, BajaActivoDetalleBE>(bajaActivoDetalle);

                Response_BajaActivoDetalle response = OperacionServiceClient.ActualizarBajaActivoDetalle(request);

                if (request.BajaActivoDetalleDTO.IDBajaActivo > 0)
                {
                    bajaActivoDetalle.IDBajaActivo = request.BajaActivoDetalleDTO.IDBajaActivo;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdBajaActivoDetalle;
            }
            catch (Exception ex)
            {
                String message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public int EliminarBajaActivoDetalle(BajaActivoDetalleBE bajaActivoDetalle)
        {
            try
            {
                Request_BajaActivoDetalle request = new Request_BajaActivoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "DelBajaActivoDetalle" };
                request.BajaActivoDetalleDTO = Mapper.Map<BajaActivoDetalleBE, BajaActivoDetalleBE>(bajaActivoDetalle);

                Response_BajaActivoDetalle response = OperacionServiceClient.EliminarBajaActivoDetalle(request);

                if (request.BajaActivoDetalleDTO.IDBajaActivo > 0)
                {
                    bajaActivoDetalle.IDBajaActivo = request.BajaActivoDetalleDTO.IDBajaActivo; 
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.DelBajaActivoDetalle;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion




























        #region TransferenciaActivos

        public IList<TransferenciaActivosBE> ListarTransferenciaActivo(TransferenciaActivosBE transActivo)
        {
            try
            {
                Request_TransferenciaActivos request = new Request_TransferenciaActivos();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListTransActivo" };
                request.TransferenciaActivosDTO = Mapper.Map<TransferenciaActivosBE, TransferenciaActivosBE>(transActivo);

                Response_TransferenciaActivos response = OperacionServiceClient.ListarTransferenciaActivo(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstTransferenciaActivos;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int RegistrarTransferenciaActivo(TransferenciaActivosBE transActivo, List<TransferenciaActivoDetalleBE> lista)
        {
            try
            {
                Request_TransferenciaActivos request = new Request_TransferenciaActivos();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "AddTransActivo" };
                request.TransferenciaActivosDTO = Mapper.Map<TransferenciaActivosBE, TransferenciaActivosBE>(transActivo);

                request.listTransActivoDetalleBE = lista.ToArray();

                Response_TransferenciaActivos response = OperacionServiceClient.RegistrarTransferenciaActivo(request);

                if (request.TransferenciaActivosDTO.IDTransfer > 0)
                {
                    transActivo.IDTransfer = request.TransferenciaActivosDTO.IDTransfer;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsTransferenciaActivos;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }



        public TransferenciaActivosBE ObtenerTransferenciaActivo(TransferenciaActivosBE transActivo)
        {
            try
            {
                Request_TransferenciaActivos request = new Request_TransferenciaActivos();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetTransActivo" };
                request.TransferenciaActivosDTO = Mapper.Map<TransferenciaActivosBE, TransferenciaActivosBE>(transActivo);

                Response_TransferenciaActivos response = OperacionServiceClient.ObtenerTransferenciaActivo(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetTransferenciaActivos;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }



        public int ActualizarTransferenciaActivo(TransferenciaActivosBE transActivo, List<TransferenciaActivoDetalleBE> list, List<TransferenciaActivoDetalleBE> toDelete)
        {
            try
            {
                Request_TransferenciaActivos request = new Request_TransferenciaActivos();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "UpdTransActivo" };
                request.TransferenciaActivosDTO = Mapper.Map<TransferenciaActivosBE, TransferenciaActivosBE>(transActivo);

                request.listTransActivoDetalleBE = list.ToArray();
                request.toDelete = toDelete.ToArray();


                Response_TransferenciaActivos response = OperacionServiceClient.ActualizarTransferenciaActivo(request);

                if (request.TransferenciaActivosDTO.IDTransfer > 0)
                {
                    transActivo.IDTransfer = request.TransferenciaActivosDTO.IDTransfer;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.UpdTransferenciaActivos;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion



        #region TransferenciaActivosDetalle

        public IList<TransferenciaActivoDetalleBE> ListarTransferenciaActivoDetalle(TransferenciaActivoDetalleBE transActivoDetalle)
        {
            try
            {
                Request_TransferenciaActivoDetalle request = new Request_TransferenciaActivoDetalle();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListTransActivoDetalle" };
                request.TransferenciaActivoDetalleDTO = Mapper.Map<TransferenciaActivoDetalleBE, TransferenciaActivoDetalleBE>(transActivoDetalle);

                Response_TransferenciaActivoDetalle response = OperacionServiceClient.ListarTransferenciaActivoDetalle(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstTransferenciaActivoDetalle;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        #endregion



        #region Sistema

        public IList<BienBE> BuscarActivos(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "SearchActivos" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.BuscarActivos(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.SearchActivos;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }

        public BienBE ObtenerSistema(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetSistema" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.ObtenerSistema(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetSistema;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public int RegistrarSistema(DetalleBienBE sistema, List<BienBE> lista)
        {
            try
            {
                Request_DetalleBien request = new Request_DetalleBien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new string[] { "AddSistema" };
                request.DetalleBienDTO = Mapper.Map<DetalleBienBE, DetalleBienBE>(sistema);

                request.LstActivos = lista.ToArray();

                Response_DetalleBien response = OperacionServiceClient.RegistrarSistema(request);

                if (request.DetalleBienDTO.IdSistema > 0)
                {
                    sistema.IdSistema = request.DetalleBienDTO.IdSistema;
                }

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.InsDetalleBien;
            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }



        public IList<DetalleBienBE> ListarSistema(DetalleBienBE sistema)
        {
            try
            {
                Request_DetalleBien request = new Request_DetalleBien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "ListSistema" };
                request.DetalleBienDTO = Mapper.Map<DetalleBienBE, DetalleBienBE>(sistema);

                Response_DetalleBien response = OperacionServiceClient.ListarSistema(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.LstDetalleBien;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        public BienBE ObtenerBienxID(BienBE bien)
        {
            try
            {
                Request_Bien request = new Request_Bien();
                request.RequestId = NewRequestId;
                request.AccessToken = AccessToken;
                request.ClientTag = ClientTag;
                request.LoadOptions = new String[] { "GetBienxID" };
                request.BienDTO = Mapper.Map<BienBE, BienBE>(bien);

                Response_Bien response = OperacionServiceClient.ObtenerBienxID(request);

                if (request.RequestId != response.CorrelationId)
                    throw new ApplicationException("Error");

                return response.GetBienxID;

            }
            catch (Exception ex)
            {
                string message = String.Format("Se encontro '{0}' excepcion en el servicio", ex.GetType().ToString());
                throw new FaultException(message);
            }
        }


        #endregion

    }
}